#include "AppFrameLib.h"

typedef struct fk_selfchk {
    UINT8  ucCmd;
    UINT32 uiData[2];
} FK_SELFCHK;

typedef struct fk_ackchk {
    UINT8  ucCmd;
    UINT32 uiChkRes;
} FK_ACKCHK;

__CODEC_EXT_CFG stCodecExtCfg[] = {
    {
        .uiExtCmd = 0x1c,
        .uiIntnCmd = 0x00,
        .uiMinSize = sizeof(FK_SELFCHK),
        .uiMaxSize = sizeof(FK_SELFCHK),
    },
};

CODEC_EXT_CFGTABLE stCfgTable = {
    .uiCfgNum  = sizeof(stCodecExtCfg)/sizeof(__CODEC_EXT_CFG),
    .pstExtCfg = stCodecExtCfg,
};

int main (int argc, char **argv)
{
    PVOID  pvUartIntf;
    PVOID  pvCodec;
    UINT8  ucCodecData[32];
    FK_ACKCHK stFkAckChk = {
        .ucCmd = 0x0c,
    };

    __INTF_UART_CFG stUartCfg = {
        .uiBaudRate  = 230400,
        .eUartBits   = __UART_BIT8,
        .eUartPartiy = __UART_PARITY_NO,
        .eUartStop   = __UART_STOP_BIT1,
        .eMode       = __UART_MODE_INT,
    };

    __CODEC_BYTEDK_CFG stProtcCfg = {
        .ucFrmHead     = {0xaa, 0x55},                                  /* 帧头格式                       */
        .ucFrmHeadLen  = 2,                                             /* 帧头长度                       */ 
        .ucFrmSizeLen  = 2,                                             /* 长度字节数                     */
        .ucCmdSize     = 1,                                             /* 命令字节数                     */
    }

    BQ_appFrameInit();
	/*
	 * 打开串口1
	 */
    pvUartIntf = BQ_intfOpen("ttyS1", __INTF_TYPE_UART, &stUartCfg);
	/*
     * 打开/创建编解码通道
     */
    pvCodec    = BQ_codecOpen("FKCodec", __CODEC_TYPE_BYTEDK, &stCfgTable);
    BQ_codecCtrl(pvCodec, __CODEC_CMD_SET_PHY, pvUartIntf);             /* 设置编解码接口                 */
    BQ_codecCtrl(pvCodec, __CODEC_CMD_SET_ENC_MAXSIZE, 32);             /* 设置编码最大缓冲区，按需设置   */
    BQ_codecCtrl(pvCodec, __CODEC_CMD_SET_PROTC, &stProtcCfg);          /* 设置编解码格式                 */
	
	BQ_codecDec(pvCodec, ucCodecData, 32);
    if (((FK_SELFCHK *)ucCodecData)->ucCmd == 0x1c) {
        stFkAckChk.uiChkRes = ((FK_SELFCHK *)ucCodecData)->uiData[0];
        printf("Recieve FK self check cmd...\n");
        BQ_codecDec(pvCodec, &stFkAckChk, sizeof(stFkAckChk));
    } else {
        printf("Recieve unknown command...\n");
    }
    
    return  (0);
}
